package database;

import java.util.List;
import java.util.Observable;

import model.Coach;
import model.Device;
import model.Member;
import model.Memento;
import model.Room;
import model.Subscription;
import model.Transaction;
import dao.CoachDao;
import dao.DeviceDao;
import dao.MemberDao;
import dao.RoomDao;
import dao.SubscriptionDao;
import daoImpl.CoachDaoImpl;
import daoImpl.DeviceDaoImpl;
import daoImpl.MemberDaoImpl;
import daoImpl.RoomDaoImpl;
import daoImpl.SubscriptionDaoImpl;
import daoImpl.TransactionDaoImpl;


public class DatabaseManager{
	
	private static DatabaseManager	instance	= null;
	private CoachDao				coachDao;
	private DeviceDao				deviceDao;
	private MemberDao				memberDao;
	private RoomDao					roomDao;
	private SubscriptionDao			subscriptionDao;
	private TransactionDaoImpl		transactionDao;
	
	
	protected DatabaseManager() {
		// Exists only to defeat instantiation.
		coachDao = new CoachDaoImpl();
		deviceDao = new DeviceDaoImpl();
		memberDao = new MemberDaoImpl();
		roomDao = new RoomDaoImpl();
		subscriptionDao = new SubscriptionDaoImpl();
		transactionDao = new TransactionDaoImpl();
	}
	
	
	public static DatabaseManager getInstance() {
		if( instance == null ){
			instance = new DatabaseManager();
		}
		return instance;
	}
	
	
	public boolean addRoom( long room_id ) {
		if( roomDao.roomExists( room_id ) ){
			return false;
		} else{
			roomDao.add( new Room() );
			return true;
		}
		
	}
	
	
	public void addMember( String name, Subscription sub ) {
		memberDao.add( new Member( name, sub ) );
	}
	
	
	public List<Subscription> getSubscriptions() {
		return subscriptionDao.getSubscriptions();
	}
	
	
	public List<Member> getMembers() {
		return memberDao.getMembers();
	}
	
	
	public List<Room> getRooms() {
		return roomDao.getRooms();
	}
	
	
	public List<Coach> getCoaches() {
		return coachDao.getCoaches();
	}
	
	
	public List<Transaction> getTransactions() {
		return transactionDao.getTransactions();
	}
	
	
	public List<Device> getDevices() {
		return deviceDao.getDevices();
	}
	
	
	public boolean addRoom( Room room ) {
		roomDao.add( new Room() );
		return true;
	}
	
	
	public void addTransaction( Transaction t ) {
		transactionDao.add( t );
	}
	
	
	public boolean addDevice( int room_nr, int device_nr ) {
		if( roomDao.roomExists( room_nr ) ){
			if( deviceDao.deviceExcists( device_nr ) ){
				Device device = new Device();
				roomDao.getRoom( room_nr ).addDevice( device );
				return true;
			}
			return true;
		} else{
			return false;
		}
	}
	
	
	public void addCoach( Coach coach ) {
		coachDao.add( coach );
	}
	
	
	public void addSubscription( Subscription sub ) {
		subscriptionDao.add( sub );
	}
	
	
	public void deleteMember( Member member ) {
		memberDao.delete( member );
	}
	
	
	public void deleteRoom( Room room ) {
		roomDao.delete( room );
	}
	
	
	public void deleteCoach( Coach coach ) {
		coachDao.delete( coach );
	}
	
	
	public void deleteSubscription( Subscription sub ) {
		subscriptionDao.delete( sub );
	}
	
	
	public Observable getTransactionDao() {
		return transactionDao;
	}
	
	
	public Memento saveToMemento() {
		return new Memento( getCoaches(), getDevices(), getMembers(), getRooms(), getSubscriptions(), getTransactions() );
	}
	
	
	public void restoreFromMemento( Memento memento ) {
		coachDao.setMemento( memento.getCoaches() );
		deviceDao.setMemento( memento.getDevices() );
		memberDao.setMemento( memento.getMembers() );
		roomDao.setMemento( memento.getRooms() );
		subscriptionDao.setMemento( memento.getSubs() );
		transactionDao.setMemento( memento.getTrans() );
	}
	
}
